package com.launchrock.projectadvisor.skeleton_BGA;

import org.andengine.engine.camera.Camera;
import org.andengine.engine.handler.timer.ITimerCallback;
import org.andengine.engine.handler.timer.TimerHandler;
import org.andengine.engine.options.EngineOptions;
import org.andengine.engine.options.ScreenOrientation;
import org.andengine.engine.options.resolutionpolicy.RatioResolutionPolicy;
import org.andengine.entity.scene.Scene;
import org.andengine.entity.scene.background.Background;
import org.andengine.entity.sprite.Sprite;
import org.andengine.opengl.texture.atlas.bitmap.BitmapTextureAtlas;
import org.andengine.opengl.texture.atlas.bitmap.BitmapTextureAtlasTextureRegionFactory;
import org.andengine.opengl.texture.region.ITextureRegion;
import org.andengine.ui.activity.BaseGameActivity;

import android.util.Log;
import android.widget.Toast;

import com.launchrock.projectadvisor.skeleton_BGA.SceneManager.AllScenes;

public class MainActivity extends BaseGameActivity {
	
	Scene scene;
	protected static final int CAMERA_WIDTH = 300;
	protected static final int CAMERA_HEIGHT = 370;
	
	BitmapTextureAtlas playerTexture;
	ITextureRegion playerTextureRegion;
	private Camera mCamera;
	private EngineOptions options;
	private SceneManager sceneManager;

	@Override
	public EngineOptions onCreateEngineOptions() {
		mCamera = new Camera( 0, 0, CAMERA_WIDTH, CAMERA_HEIGHT); 
		options = new EngineOptions(true, ScreenOrientation.PORTRAIT_FIXED, 
				new RatioResolutionPolicy(CAMERA_WIDTH,CAMERA_HEIGHT), mCamera);
		return options;
	}

	@Override
	public void onCreateResources(
			OnCreateResourcesCallback pOnCreateResourcesCallback)
			throws Exception {
		
		sceneManager = new SceneManager(this, mEngine , mCamera);
		sceneManager.loadSplashResources();
		
		//loadGfx();
		pOnCreateResourcesCallback.onCreateResourcesFinished();
		
	}
	
	private void loadGfx(){
		BitmapTextureAtlasTextureRegionFactory.setAssetBasePath("gfx/");
		playerTexture = new BitmapTextureAtlas(getTextureManager(), 64, 64);
		playerTextureRegion = BitmapTextureAtlasTextureRegionFactory.createFromAsset(playerTexture, this, "player.png",0,0);
		playerTexture.load();
	}

	@Override
	public void onCreateScene(OnCreateSceneCallback pOnCreateSceneCallback)
			throws Exception {
		
		pOnCreateSceneCallback.onCreateSceneFinished(sceneManager.createSplashScene());
		
		//Log.i("camera popo"," height = "+this.mRenderSurfaceView.getHeight()+" width = "+this.mRenderSurfaceView.getWidth());
		//+this.mRenderSurfaceView.getHeight()+" width = "+this.mRenderSurfaceView.getWidth()
		//this.mRenderSurfaceView.getHeight();
	}

	@Override
	public void onPopulateScene(Scene pScene,
			OnPopulateSceneCallback pOnPopulateSceneCallback) throws Exception {
		final int sHeight = this.mRenderSurfaceView.getHeight() , sWidth = this.mRenderSurfaceView.getWidth();
		mEngine.registerUpdateHandler(new TimerHandler(3f, new ITimerCallback(){
			public void onTimePassed(final TimerHandler pTimerHandler)
			{
				mEngine.unregisterUpdateHandler(pTimerHandler);
				sceneManager.loadGameResources();
				sceneManager.createGameScene(sHeight,sWidth);
				sceneManager.setCurrentScene(AllScenes.GAME);
			}
		}));
		pOnPopulateSceneCallback.onPopulateSceneFinished();
		
	}
	
	
}
